<?

class Container extends PHLARE_Model
{
	
	public function __construct($id = null, $version = null, $parent_id = null, $left_val = null, $right_val = null, $stage_state = null)
	{
		parent::__construct();
		
		$this->id = $id;
		$this->parent_id = $parent_id;
		$this->left_val = $left_val;
		$this->right_val = $right_val;
		$this->stage_state = $stage_state;
		
		$db_fields = array(
			'id' => new Auto_field(
				array(
					'field_name' => 'id'
				)
			),
			'object_id' => new Integer_field(
				array(
					'field_name' => 'object_id',
					'unsigned' => true,
					'index' => true
				)
			),
			'object_version' => new Integer_field(
				array(
					'field_name' => 'object_version',
					'unsigned' => true,
					'index' => true
				)
			),
			'edit_comments' => new Text_field(
				array(
					'field_name' => 'edit_comments',
					'help_text' => 'Some helpful information to describe what was done in this edit.'
				)
			)
		);
		
		$this->initialize_db_fields($db_fields);
		
		$this->add_field_to_tab('Content', 'edit_comments');
	} // end __construct
	
	
	protected function populate($id, $version)
	{
		$query = $this->db->get_where(
			$this->db_table,
			array(
				'object_id' => $id,
				'object_version' => $version
			)
		);
		
		if($query->num_rows() > 0)
		{
			$row = $query->result_array();
			
			foreach($row[0] as $key => $value)
			{
				$this->_fields[$key]->set_value($value);
			}
		}
		
		$this->populate_versions($id);
	} // end populate
	
	
	protected function populate_versions($id)
	{
		$this->db->select('id, object_version, edit_comments');
		$query = $this->db->get_where(
			$this->db_table,
			array(
				'object_id' => $id
			)
		);
		
		if($query->num_rows() > 0)
		{
			$row = $query->result_array();
			
			foreach($row as $version_info)
			{
				$this->_versions[ $version_info['object_version'] ] = $version_info['edit_comments'];
			}
		}
	} // end populate_versions
	
	
	public function update($data)
	{
		// Get the next version id
		$next_object_version = $this->get_next_object_version();
		
		// Only take the data for which we have a corresponding field element.
		foreach($this->_fields as $key => $value)
		{
			if(isset($data[$key]))
			{
				$this->_fields[$key]->set_value($data[$key]);
			}
		}
		
		// Create a "new" page with all the updated data with a new version number
		$this->_fields['object_version']->set_value(
			$next_object_version
		);
		
		unset($this->_fields['id']);
		
		$data = array();
		foreach($this->_fields as $key => $field)
		{
			$data[ $field->get_field_name() ] = $field->get_value();
		}
		
		$this->db->insert($this->db_table, $data);
		
		return $next_object_version;
		
	} // end update
	
	
	/**
	 * create:
	 * Create a DB row.
	 * 
	 * $data: POST data
	 */
	public function create($data)
	{
	    $return_array = array(
	        'object_id' => $this->get_next_object_id()
	    );
	    
	    $this->_fields['object_id']->set_value($return_array['object_id']);
		$return_array['version_id'] = $this->update($data);
		
		return $return_array;
		
	} // end create
	
	
	protected function get_next_object_id()
	{
		$this->db->select_max('object_id');
		$query = $this->db->get($this->db_table);
		
		// Default case... no current objects in the DB table
		$object_id = 0;
		
		if($query->num_rows() > 0) {
		    $row = $query->row();
		    $object_id = $row->object_id;
		}
		
		return $object_id + 1;
	} // end get_next_object_version
	
	
	protected function get_next_object_version()
	{
		$this->db->select_max('object_version');
		$this->db->where('object_id', $this->_fields['object_id']->get_value());
		$query = $this->db->get($this->db_table);
		
		// Default case... no current version
		$object_version = 0;
		
		if($query->num_rows() > 0) {
		    $row = $query->row();
		    $object_version = $row->object_version;
		}
		
		return $object_version + 1;
	} // end get_next_object_version
	

} // end Container